//
//  BRCustiomAlertView.m
//  BrezzeSaaS
//
//  Created by IOS on 2021/10/14.
//  Copyright © 2021 Brezze GWG. All rights reserved.
//

#import "BRCustiomAlertView.h"

@implementation BRCustiomAlertView

- (instancetype)initWithTitle:(NSString *)title subTitle:(NSString *)subTitle cancelStr:(NSString *)cancelStr succStr:(NSString *)succStr cancelAction:(AlertAction)cancel successAction:(AlertAction)success showBC:(BOOL)isHas
{
    self = [super init];
    if (self) {
        self.cancelAction = cancel;
        self.successAction = success;
        self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        [self setTitle:title subTitle:subTitle cancelStr:cancelStr succStr:succStr showBC:isHas];
    }
    return self;
}

- (instancetype)initWithTitle:(NSString *)title subTitle:(NSString *)subTitle cancelStr:(NSString *)cancelStr succStr:(NSString *)succStr cancelAction:(AlertAction)cancel successAction:(AlertAction)success {
    return [self initWithTitle:title subTitle:subTitle cancelStr:cancelStr succStr:succStr cancelAction:cancel successAction:success showBC:NO];
}

- (instancetype)initWithTitle:(NSString *)title subTitle:(NSString *)subTitle cancelAction:(AlertAction)cancel successAction:(AlertAction)success showBC:(BOOL)isHas {
    
    return [self initWithTitle:title subTitle:subTitle cancelStr:Localized(@"public_notNow") succStr:Localized(@"public_yes") cancelAction:cancel successAction:success showBC:isHas];
}

- (instancetype)initWithTitle:(NSString *)title subTitle:(NSString *)subTitle cancelAction:(AlertAction)cancel successAction:(AlertAction)success {
    return [self initWithTitle:title subTitle:subTitle cancelStr:Localized(@"public_notNow") succStr:Localized(@"public_yes") cancelAction:cancel successAction:success showBC:NO];
}


- (void)cancel {
    
    if (self.cancelAction) {
        self.cancelAction();
    }
    
    [self removeSelf];
}

- (void)succ {
    if (self.successAction) {
        self.successAction();
    }
    if (!self.isAllways) {
        [self removeSelf];
    }
    
}

- (void)removeSelf {
    
    dispatch_async(dispatch_get_main_queue(), ^{
        self.hidden = YES;
        
        [self removeFromSuperview];
    });
   
}


- (void)setTitle:(NSString *)title subTitle:(NSString *)subTitle cancelStr:(NSString *)cancelStr succStr:(NSString *)succStr showBC:(BOOL)isHas {
    
    self.backgroundColor = UIColorClear;
    
    if (isHas) {
//        UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
//        UIVisualEffectView *shadowView = [[UIVisualEffectView alloc] initWithEffect:effect];
        UIView *shadowView = [[UIView alloc] init];
        shadowView.backgroundColor = UIColorMakeWithRGBA(0, 0, 0, 0.2);
        [self addSubview:shadowView];
        [shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
    }
    
    CGFloat cont_w = kScreenWidth - 56;
    UIView *contentView  = [[UIView alloc] init];
    contentView.backgroundColor = UIColor.qd_backgroundColor3;
    contentView.layer.cornerRadius = kCorners;
    contentView.layer.masksToBounds  = YES;
    [self addSubview:contentView];
    
    QMUILabel *titleL = [BRToolsView br_label:title textColor:kColorTitle font:[UIFont fontB:17]];
    titleL.numberOfLines = 0;
    titleL.textAlignment = NSTextAlignmentCenter;
    
    QMUILabel *subTitleL = [BRToolsView br_label:subTitle textColor:kColorTitle font:[UIFont fontM:14]];
    subTitleL.numberOfLines  = 0;
    subTitleL.textAlignment = NSTextAlignmentCenter;
    
    UIStackView *stack = [[UIStackView alloc] init];
    stack.axis = UILayoutConstraintAxisVertical;
    stack.spacing = 14;
    [stack addArrangedSubview:titleL];
    [stack addArrangedSubview:subTitleL];
    [contentView addSubview:stack];
    [stack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(contentView).offset(16);
        make.right.equalTo(contentView).offset(-16);
        make.top.equalTo(contentView).offset(20);
    }];
    
    titleL.hidden = title.length == 0;
    subTitleL.hidden = subTitle.length == 0;
//    if (title.length != 0) {
//        [contentView addSubview:titleL];
//        [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(contentView).offset(16);
//            make.right.equalTo(contentView).offset(-16);
//            make.top.equalTo(contentView).offset(10);
//        }];
//    }
//
//    QMUILabel *nextL = titleL;
//    if (subTitle.length != 0) {
//        nextL = subTitleL;
//
//        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:subTitle];
//        str.yy_lineSpacing = 3.5;
//        str.yy_alignment = NSTextAlignmentCenter;
//        subTitleL.attributedText = str;
//
//        [contentView addSubview:subTitleL];
//
//        [subTitleL mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(contentView).offset(16);
//            make.right.equalTo(contentView).offset(-16);
//            if (title.length != 0) {
//                make.top.equalTo(titleL.mas_bottom).offset(18);
//            }else {
//                make.top.equalTo(contentView).offset(16);
//            }
//
//        }];
//    }
    
    
    UIView *bottmView = [UIView new];
    bottmView.backgroundColor = UIColor.qd_backgroundColor3;
    [contentView addSubview:bottmView];
    [bottmView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(52);
        make.width.mas_equalTo(cont_w);
        make.bottom.equalTo(contentView);
        make.top.equalTo(stack.mas_bottom).offset(18);
    }];
    
    QMUIButton *cancleBtn = [[QMUIButton alloc] init];
    [cancleBtn setTitle:cancelStr forState:UIControlStateNormal];
    [cancleBtn setTitleColor:kColorMain forState:UIControlStateNormal];
    cancleBtn.titleLabel.font = [UIFont br_fontM:14];
    
    QMUIButton *succBtn = [[QMUIButton alloc] init];
    [succBtn setTitle:succStr forState:UIControlStateNormal];
    [succBtn setTitleColor:kColorMain forState:UIControlStateNormal];
    succBtn.titleLabel.font = [UIFont br_fontB:14];
    
    if (cancelStr.length != 0) {
        
        [bottmView addSubview:cancleBtn];
        
        CGFloat btn_w = succStr.length != 0 ? cont_w / 2.0 : cont_w;
        [cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(52);
            make.width.mas_equalTo(btn_w);
            make.left.bottom.equalTo(bottmView);
        }];
        [cancleBtn addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    }
    
    if (succStr.length != 0) {
        
        [bottmView addSubview:succBtn];
        
        CGFloat btn_w = cancelStr.length != 0 ? cont_w / 2.0 : cont_w;
        [succBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(52);
            make.width.mas_equalTo(btn_w);
            make.right.bottom.equalTo(bottmView);
        }];
        
        [succBtn addTarget:self action:@selector(succ) forControlEvents:UIControlEventTouchUpInside];
    }
    
    UIView *line = [UIView new];
    line.backgroundColor = UIColor.qd_lineColor2;
    [bottmView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kScreenWidth - 56);
        make.height.mas_equalTo(0.5);
        make.centerX.equalTo(bottmView);
        make.top.equalTo(bottmView);
    }];
    
    if (cancelStr.length != 0 && succStr.length != 0) {
        UIView *line = [UIView new];
        line.backgroundColor = UIColor.qd_lineColor2;
        [bottmView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(1);
            make.height.mas_equalTo(30);
            make.centerX.centerY.equalTo(bottmView);
        }];
    }
    
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.equalTo(self);
        make.width.mas_equalTo(cont_w);
    }];
    
    [self layoutSubviews];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
